package frame;

import clsaa.Biglotto;
import data.ConfigBigLotto;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ShowTableFrame{
    ConfigBigLotto configbigLotto;
    private static JTable table;
    JScrollPane scrollPane;
    // 自定义单元格渲染器
    private class CustomCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // 设置默认的背景颜色（例如，浅灰色）
            setBackground(Color.orange);

            // 如果需要，您可以根据行或列索引、值等设置不同的颜色
            // 例如，如果某一列的值大于某个阈值，则设置不同的颜色
            return this;
        }
    }
    // 自定义单元格渲染器
    private class CustomCellRenderer1 extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // 设置默认的背景颜色（例如，浅灰色）
            setBackground(Color.white);

            // 如果需要，您可以根据行或列索引、值等设置不同的颜色
            // 例如，如果某一列的值大于某个阈值，则设置不同的颜色
            return this;
        }
    }
    private class CustomCellRenderer2 extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // 设置默认的背景颜色（例如，浅灰色）
            setBackground(Color.YELLOW);

            // 如果需要，您可以根据行或列索引、值等设置不同的颜色
            // 例如，如果某一列的值大于某个阈值，则设置不同的颜色
            return this;
        }
    }
    private class CustomCellRenderer3 extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // 设置默认的背景颜色（例如，浅灰色）
            setBackground(Color.LIGHT_GRAY);

            // 如果需要，您可以根据行或列索引、值等设置不同的颜色
            // 例如，如果某一列的值大于某个阈值，则设置不同的颜色
            return this;
        }
    }
    private class CustomCellRenderer4 extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // 设置默认的背景颜色（例如，浅灰色）
            setBackground(Color.RED);

            // 如果需要，您可以根据行或列索引、值等设置不同的颜色
            // 例如，如果某一列的值大于某个阈值，则设置不同的颜色
            return this;
        }
    }
    private class ButtonRenderer extends JButton implements TableCellRenderer {
        public ButtonRenderer() {
            setOpaque(true);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            setText((value == null) ? "" : value.toString());
//            System.out.println(value.toString()+"//"+value);
            return this;
        }
    }

    private class ButtonEditor extends DefaultCellEditor {
        private JButton button;

        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
            button = new JButton("Delete");
            button.setOpaque(true);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int row = table.convertRowIndexToModel(table.getEditingRow());
                    // Perform deletion action here based on the row index
                    // For example:
                    DefaultTableModel model = (DefaultTableModel) table.getModel();
//                    System.out.println(row);
                    if(row==0){

                    }else{
                        configbigLotto.getAllbiglotto().getAllData().remove(row-1);
                        configbigLotto.SaveBiglottoINFILE();
                        model.removeRow(row);
                    }

                }
            });
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            return button;
        }
    }
//    private class ButtonEditor11 extends DefaultCellEditor {
//        private JButton button;
//
//        public ButtonEditor11(JCheckBox checkBox) {
//            super(checkBox);
//            button = new JButton("改");
//            button.setOpaque(true);
//            button.addActionListener(new ActionListener() {
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    int row = table.convertRowIndexToModel(table.getEditingRow());
//                    // Perform deletion action here based on the row index
//                    // For example:
//                    DefaultTableModel model = (DefaultTableModel) table.getModel();
////                    System.out.println(row);
////                    configbigLotto.getAllbiglotto().getAllData().remove(row);
////                    configbigLotto.SaveBiglottoINFILE();
//
//                    Biglotto biglotto=configbigLotto.getAllbiglotto().getAllData().get(row);
//
//
//
//                    model.removeRow(row);
//                }
//            });
//        }

//        @Override
//        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
//            return button;
//        }
//    }
    public JTable BigLottoDataTable(){

        // 创建表格模型
        DefaultTableModel model = new DefaultTableModel();
        configbigLotto=new ConfigBigLotto();
        configbigLotto.getAllbiglotto().reorderDescend();
        // 添加表头
        model.addColumn("期号");
        model.addColumn("日期");
        for(int i=0;i<35;++i)model.addColumn(i+1);
        for(int i=0;i<12;++i)model.addColumn(i+1);
//        for(int i=0;i<12;++i)model.addColumn(i+1);
        model.addColumn("功能");
//        model.addColumn("功能");
        Object[] object=new Object[50];
        object[0]=configbigLotto.getAllbiglotto().getAllData().size();
        object[1]="总数";
        for(int i=2,j=0;i<37;++i,++j){
            object[i]=configbigLotto.getAllbiglotto().getDataA()[j];
        }
        for(int i=37,j=0;i<49;++i,++j){
            object[i]=configbigLotto.getAllbiglotto().getDataB()[j];
        }
        model.addRow(object);
//        object=new Object[50];
//        object[0]="次数/总数";
//        object[1]="概率";
//        for(int i=2,j=0;i<37;++i,++j){
//            object[i]=configbigLotto.getAllbiglotto().getDataAV()[j];
//        }
//        for(int i=37,j=0;i<49;++i,++j){
//            object[i]=configbigLotto.getAllbiglotto().getDataBV()[j];
//        }
//        model.addRow(object);
        for(Biglotto lotto:configbigLotto.getAllbiglotto().getAllData()){
            object=new Object[50];
            object[0]=lotto.getId();
            object[1]=lotto.getDate();
            object[lotto.getA()+1]=lotto.getA();
            object[lotto.getB()+1]=lotto.getB();
            object[lotto.getC()+1]=lotto.getC();
            object[lotto.getD()+1]=lotto.getD();
            object[lotto.getE()+1]=lotto.getE();
            object[lotto.getF()+36]=lotto.getF();
            object[lotto.getG()+36]=lotto.getG();
//            JButton dele=new JButton("删");
//            dele.addActionListener(new ActionListener() {
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    // 取消按钮的逻辑（例如关闭窗口）
//                    System.out.println("xxxxxxxxxxx");
//                    dispose();
//                }
//            });
//            object[61]=dele;
            object[49]="删";
//            object[50]="改";
            model.addRow(object);
        }
        table = new JTable(model);

        table.getColumnModel().getColumn(49).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(49).setCellEditor(new ButtonEditor(new JCheckBox()));

        table.getColumnModel().getColumn(0).setCellRenderer(new CustomCellRenderer());
        table.getColumnModel().getColumn(1).setCellRenderer(new CustomCellRenderer());
        for(int i=2;i<7;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer1());
        for(int i=7;i<12;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer3());
        for(int i=12;i<17;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer1());
        for(int i=17;i<22;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer3());
        for(int i=22;i<27;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer1());
        for(int i=27;i<32;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer3());

        for(int i=37;i<49;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer2());
//        for(int i=49;i<61;++i)table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellRenderer3());

        table.getColumnModel().getColumn(49).setCellRenderer(new CustomCellRenderer4());

        return table;
    }
    public ShowTableFrame(){
//        super("大乐透历史记录");
//        this.setSize(1900, 1000);
//        this.setBounds(10,40,1900,1000);
//        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = BigLottoDataTable();

//        table.setDefaultRenderer(Object.class, new CustomCellRenderer()); // 将自定义渲染器应用到所有列

//        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // 关闭自动调整
//        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); // 关闭自动调整
        JScrollPane scrollPane = new JScrollPane(table);
        this.scrollPane=scrollPane;
        this.scrollPane.setBounds(0,0,1900,940);
//        this.add(scrollPane);
//        this.setVisible(true);

    }

    public static void main(String[] args) {
        new ShowTableFrame();
    }
}
